// Copyright (C) 2007 Conrad Tran
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace SleepTimer
{
    public partial class SleepForm : Form
    {
        [DllImport("user32.dll")]
        private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);

        public int WM_SYSCOMMAND = 0x0112;
        public int SC_MONITORPOWER = 0xF170;

        private int _Hours;
        private int _Minutes;

        public SleepForm()
        {
            InitializeComponent();
            TimeComBox.Items.Add("0   Minute");
            TimeComBox.Items.Add("5   Minutes");
            TimeComBox.Items.Add("10  Minutes");
            TimeComBox.Items.Add("15  Minutes");
            TimeComBox.Items.Add("20  Minutes");
            TimeComBox.Items.Add("30  Minutes");
            TimeComBox.Items.Add("45  Minutes");
            TimeComBox.Items.Add("60  Minutes");
            TimeComBox.Items.Add("120 Minutes");
            TimeComBox.Items.Add("180 Minutes");
            TimeComBox.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CalculateHoursMinute();
            if (backgroundWorker1.IsBusy)
            {
                backgroundWorker1.CancelAsync();
                button1.Text = "Sleep";
            }
            else
            {
                backgroundWorker1.RunWorkerAsync();
                button1.Text = "Cancel";
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            int hours = Hours;
            int minutes = Minutes;

            int sleepTime = 0;

            if (hours > 0)
                sleepTime = hours*60;
            if (minutes > 0)
                sleepTime += minutes;

            sleepTime = sleepTime*60;

            for (int i = sleepTime; (i > 0) && !worker.CancellationPending; --i)
            {
                worker.ReportProgress(i);
                Thread.Sleep(1000);
            }

            if (!worker.CancellationPending)
                worker.ReportProgress(0);
            else
                e.Cancel = true;
        }

        public int Hours
        {
            get { return _Hours; }

            set { _Hours = value; }
        }

        public int Minutes
        {
            get { return _Minutes; }
            set { _Minutes = value; }
        }

        private void CalculateHoursMinute()
        {
            string s = TimeComBox.SelectedItem as String;
            _Hours = Convert.ToInt32(s.Substring(0, s.IndexOf(' ')))/60;
            _Minutes = Convert.ToInt32(s.Substring(0, s.IndexOf(' ')))%60;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            button1.Text = "Sleep";

            if (!e.Cancelled)
            {
                SendMessage(Handle.ToInt32(), WM_SYSCOMMAND, SC_MONITORPOWER, 2);
            }
            else
            {
                CountDownTextBox.Text = "Cancelled";
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            CountDownTextBox.Text = e.ProgressPercentage.ToString();
        }
    }
}